<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
		<link href="../cssmui/mui.min.css" rel="stylesheet" />
		<script type="text/javascript" src="../hyl/mui/mui.js"></script>
		<script type="text/javascript" src="../mui/common.js"></script>
		<script type="text/javascript" src="../hyl/util/core.js"></script>
		<script type="text/javascript" src="../hyl/util/core.arr.js"></script>
		<script type="text/javascript" src="../hyl/plus/sqlite.web.js"></script>
		<script type="text/javascript" src="../hyl/md/sql-modeldata.js"></script>
		<script type="text/javascript" src="./designsql.js"></script>
		<script type="text/javascript" src="./ini.dbmanager.js"></script>

		<!-- 	<script type="text/javascript" src="zsql.js"></script> -->


	</head>

	<body>
		<header class="mui-bar mui-bar-nav">

			<h1 class="mui-title">后台管理</h1>

		</header>
		<div class="mui-content">
			<div class="mui-row">
				<div class="mui-col-xs-12 mui-col-sm-4">
					表<select id="select1">
						<option value="all">所有</option>
					</select>
				</div>
				<div class="mui-col-xs-12 mui-col-sm-4">
					<div class="mui-plus-hidden">
						模板<select id="select2" style="background-color: #bCDcba;">
							<option value="">请选择</option>
							<option value="0">置空</option>
							<option value="1" selected>obj</option>
						</select>
					</div>
				</div>
				<div class="mui-col-xs-12 mui-col-sm-4">
					分割符
					<select id="select3" width="120px">
						<option value="">请选择</option>
						<option value="rn" selected>换行</option>
						<option value=",">逗号+换行</option>
						<option value=";rn">分号+换行</option>
					</select>
				</div>
			</div>

			过滤参数：<input type="text" id="rpobj" value="" /><br>
			<button class="mui-plus-hidden" id="bt1">导出字段模型</button>
			<button id="btndrop">删除表</button>
			<button id="btnrestbs">重建表</button>
			<button id="btncrtbs">创建表</button>
			<button id="btnidx">建索引</button>
			<button id="btnins">插入数据</button>
			<button id="btnupd">修改表数据</button>
			<button id="btnsel">查询某表</button>
			<button id="btnmdl">建模</button>
			<button id="btnbatch">批量执行</button>
			<br> <button id="btncp">copy</button>
			输出：<br>
			<textarea id="text1" rows="20" cols="38"></textarea><br>

		</div>
		<div>
			<button id="bt11">初始化字典</button>
		</div>
		<script type="text/javascript">
			var _DB = new DataBase('mydb');

			//console.log(a);
			var _DBM = new DBManager(_DBconfig);
			var oSel = document.getElementById("select1");
			var omc = document.getElementById("select2");
			var osp = document.getElementById("select3");
			var text1 = document.getElementById("text1");
			var rpobj = document.getElementById("rpobj");
			var tablesarr = getConfig.tableNames;

			for (var i = 0; i < tablesarr.length; i++) {
				var oPt = new Option(tablesarr[i], tablesarr[i]); //创建一个option
				oSel.add(oPt); //添加
			}
			var modelconfs = [];
			modelconfs[0] = "%%field0 :\'\'";
			modelconfs[1] = "%%field0 :%%value";


			document.getElementById("bt1").addEventListener('click', function() {
				var tablename = oSel.options[oSel.selectedIndex].value;
				var i = omc.options[omc.selectedIndex].value;
				var sp = osp.options[osp.selectedIndex].value;

				sp = sp.replaceAll("rn", '\r\n')
				var s = ''
				if (tablename == 'all') {
					for (var j in _DBconfig) {
						var table = _DBconfig[j];
						s = s + j + ':{\n';
						s = s + _FD.mergeFields(table, modelconfs[i], sp);
						s = s + '\n},\n\n';
					}
				} else {
					var s = _FD.mergeFields(getConfig.getTable(tablename), modelconfs[i], sp);
				}
				//console.log(s)
				text1.value = s;
			});

			document.getElementById("btndrop").addEventListener('click', function() {
				var tablename = oSel.options[oSel.selectedIndex].value;
				var arr = [];
				if (tablename == 'all') {
					arr = _DBM.dropAll(tablename);
				} else {
					var obj = rpobj.value;
					var table = getConfig.getTable(tablename);
					arr = _DBM.dropTable(table, obj);
				}
				text1.value = arr.join("");
				//_DB.transaction(arr);
				_DB.batch(arr);
			});
			document.getElementById("btnrestbs").addEventListener('click', function() {
				var arr = _DBM.resetAll();
				text1.value = arr.join("");
				//console.log(arr)
				_DB.batch(arr);
			});
			document.getElementById("btncrtbs").addEventListener('click', function() {
				var tablename = oSel.options[oSel.selectedIndex].value;
				if (tablename == 'all') {
					var arr = _DBM.createAll(tablename);
					text1.value = arr.join("");
					_DB.batch(arr);
				} else {
					var obj = rpobj.value;
					var table = getConfig.getTable(tablename);
					text1.value = _DBM.createTable(table, obj);
					_DB.exec(text1.value);
				}

			});
			//{"id":1}
			document.getElementById("btnins").addEventListener('click', function() {
				var tablename = oSel.options[oSel.selectedIndex].value;
				var sp = osp.options[osp.selectedIndex].value;
					sp = sp.replaceAll("rn", '\r\n')
				var arr = [];
				if (tablename == 'all') {
					arr = _DBM.insertAll();
					text1.value = arr.join(sp);
					_DB.batch(arr);
				} else {
					var table = getConfig.getTable(tablename);
					var obj = rpobj.value;
					//console.log("btnins", obj)
					if (!_IS.isEmptyStr(obj) && obj != '') {
						var map, len;
						//console.log('map1', obj)
						map = JSON.parse(obj);
						//console.log('map2', map)
						if (obj.indexOf('@inc') > -1) {
							var i = 0;
							var l = 100;
							for (; i < l; i++) {
								var map1 = _Fun.o.clone(map);
								for (var key in map1) {
									//console.log('map', map1)
									map1[key] = map1[key].replaceAll('@inc', i)
								}
							//console.log('map', map1)
								arr.push(_DBM.insertRecord(table, map1))
							}
						} else {
							arr.push(_DBM.insertRecord(table, map))
						}
						text1.value = arr.join(sp);
						_DB.batch(arr);
					}


				}
			});
			document.getElementById("btnupd").addEventListener('click', function() {
				var tablename = oSel.options[oSel.selectedIndex].value;
				if (tablename == 'all') {
					return;
				} else {
					var table = getConfig.getTable(tablename);
					var obj = rpobj.value;
					text1.value = _DBM.updateRecord(table, obj);
					_DB.exec(text1.value);
				}
			});
				document.getElementById("btnbatch").addEventListener('click', function() {
					var arr=text1.value.split(';')
					_DB.batch(arr);
			});
			document.getElementById("btnsel").addEventListener('click', function() {
				var tablename = oSel.options[oSel.selectedIndex].value;
				if (tablename == 'all') {
					text1.value = "";
					for (var i = 0; i < _DBconfig.length; i++) {
						var table = _DBconfig[i];
						_DB.query(_DBM.selectTable(table), function(rs, sql) {
							text1.value += "\r\n" + sql + "\r\n" + JSON.stringify(rs);
						});
					}
				} else {
					var table = getConfig.getTable(tablename);
					_DB.query(_DBM.selectTable(table), function(rs) {
						text1.value = JSON.stringify(rs);
					});
				}
			});

			document.getElementById("btnidx").addEventListener('click', function() {
				var tablename = oSel.options[oSel.selectedIndex].value;
				if (tablename == 'all') {
					var arr = _DBM.indexAll(tablename);
					text1.value = arr.join("");
					_DB.batch(arr);
				} else {
					text1.value = _DBM.indexTable(tablename);
					_DB.exec(text1.value);
				}
			});

			document.getElementById("btnmdl").addEventListener('click', function() {
				var tablename = oSel.options[oSel.selectedIndex].value;
				if (tablename == 'all') {
					text1.value = _DBM.modelAll(tablename);
				} else {
					text1.value = _DBM.createModel(tablename);
				}
			});
			document.getElementById("btncp").addEventListener('click', function() {
				var e = document.getElementById("text1");
				e.select(); // 选择对象
				document.execCommand("Copy");
			});
		</script>
	</body>

</html>
